<!DOCTYPE HTML>
<html lang="zh-Hans" class="sidebar-visible no-js light">
    <head>
        <!-- Book generated using mdBook -->
        <meta charset="UTF-8">
        <title>进击的Rust多线程——Send与Sync - Rust 优秀博文</title>


        <!-- Custom HTML head -->
        
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="theme-color" content="#ffffff" />

        <link rel="icon" href="../../favicon.svg">
        <link rel="shortcut icon" href="../../favicon.png">
        <link rel="stylesheet" href="../../css/variables.css">
        <link rel="stylesheet" href="../../css/general.css">
        <link rel="stylesheet" href="../../css/chrome.css">
        <link rel="stylesheet" href="../../css/print.css" media="print">

        <!-- Fonts -->
        <link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
        <link rel="stylesheet" href="../../fonts/fonts.css">

        <!-- Highlight.js Stylesheets -->
        <link rel="stylesheet" href="../../highlight.css">
        <link rel="stylesheet" href="../../tomorrow-night.css">
        <link rel="stylesheet" href="../../ayu-highlight.css">

        <!-- Custom theme stylesheets -->

    </head>
    <body>
        <!-- Provide site root to javascript -->
        <script type="text/javascript">
            var path_to_root = "../../";
            var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
        </script>

        <!-- Work around some values being stored in localStorage wrapped in quotes -->
        <script type="text/javascript">
            try {
                var theme = localStorage.getItem('mdbook-theme');
                var sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }
        </script>

        <!-- Set the theme before any content is loaded, prevents flash -->
        <script type="text/javascript">
            var theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
            if (theme === null || theme === undefined) { theme = default_theme; }
            var html = document.querySelector('html');
            html.classList.remove('no-js')
            html.classList.remove('light')
            html.classList.add(theme);
            html.classList.add('js');
        </script>

        <!-- Hide / unhide sidebar before it is displayed -->
        <script type="text/javascript">
            var html = document.querySelector('html');
            var sidebar = 'hidden';
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            }
            html.classList.remove('sidebar-visible');
            html.classList.add("sidebar-" + sidebar);
        </script>

        <nav id="sidebar" class="sidebar" aria-label="Table of contents">
            <div class="sidebar-scrollbox">
                <ol class="chapter"><li class="chapter-item expanded "><a href="../../empty.html"><strong aria-hidden="true">1.</strong> 类型系统</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../01_类型系统/Rust_Concept_Clarification_Deref_vs_AsRef_vs_Borrow_vs_Cow/Rust_Concept_Clarification_Deref_vs_AsRef_vs_Borrow_vs_Cow.html"><strong aria-hidden="true">1.1.</strong> Rust Concept Clarification Deref vs AsRef vs Borrow vs Cow</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/Rust_Concept_Clarification_Deref_vs_AsRef_vs_Borrow_vs_Cow/Deref_AsRef_Borrow_Cow释义.html"><strong aria-hidden="true">1.2.</strong> Deref AsRef Borrow Cow 释义</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/Rust的Borrow和AsRef：让你的代码用起来像呼吸一样自然/Rust的Borrow和AsRef：让你的代码用起来像呼吸一样自然.html"><strong aria-hidden="true">1.3.</strong> Rust的Borrow和AsRef：让你的代码用起来像呼吸一样自然</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/Rust的Cow类型有什么用？详解Cow及其用途/Rust的Cow类型有什么用？详解Cow及其用途.html"><strong aria-hidden="true">1.4.</strong> Rust的Cow类型有什么用？详解Cow及其用途</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/判别Fn、FnMut、FnOnce的标准/判别Fn、FnMut、FnOnce的标准.html"><strong aria-hidden="true">1.5.</strong> 判别Fn、FnMut、FnOnce的标准</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/一行代码告诉你内部可变性的真相(UnsafeCell)/一行代码告诉你内部可变性的真相(UnsafeCell).html"><strong aria-hidden="true">1.6.</strong> 一行代码告诉你内部可变性的真相(UnsafeCell)</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/Tour_of_Rust's_Standard_Library_Traits/Tour_of_Rust's_Standard_Library_Traits.html"><strong aria-hidden="true">1.7.</strong> Tour of Rust's Standard Library Traits</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/逆变、协变与子类型，以及Rust/逆变、协变与子类型，以及Rust.html"><strong aria-hidden="true">1.8.</strong> 逆变、协变与子类型，以及Rust</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/Rust自引用结构、Pin与Unpin/Rust自引用结构、Pin与Unpin.html"><strong aria-hidden="true">1.9.</strong> Rust自引用结构、Pin与Unpin</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/为什么Rust需要Pin,Unpin/为什么Rust需要Pin,Unpin.html"><strong aria-hidden="true">1.10.</strong> 译：为什么 Rust 需要 Pin, Unpin ？</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/定海神针Pin和Unpin/定海神针Pin和Unpin.html"><strong aria-hidden="true">1.11.</strong> 译：定海神针 Pin 和 Unpin</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/sizedness-in-rust/sizedness-in-rust.html"><strong aria-hidden="true">1.12.</strong> Sizedness in Rust</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/Rust生命周期集大成者PhantomData〈T〉/Rust生命周期集大成者PhantomData〈T〉.html"><strong aria-hidden="true">1.13.</strong> Rust生命周期集大成者 PhantomData&lt;T&gt;</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/数据库表达式执行的黑魔法：用Rust做类型体操/数据库表达式执行的黑魔法：用Rust做类型体操_Part_0.html"><strong aria-hidden="true">1.14.</strong> 数据库表达式执行的黑魔法：用Rust做类型体操 Part 0</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/数据库表达式执行的黑魔法：用Rust做类型体操/数据库表达式执行的黑魔法：GAT实现引用类型关联_Part_1.html"><strong aria-hidden="true">1.15.</strong> 数据库表达式执行的黑魔法：GAT实现引用类型关联 Part 1</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/数据库表达式执行的黑魔法：用Rust做类型体操/数据库表达式执行的黑魔法：用HRTB写bound_Part_2.html"><strong aria-hidden="true">1.16.</strong> 数据库表达式执行的黑魔法：用HRTB写bound Part 2</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/数据库表达式执行的黑魔法：用Rust做类型体操/数据库表达式执行的黑魔法：用Rust做类型体操之用宏展开重复代码_Part_3_&_4.html"><strong aria-hidden="true">1.17.</strong> 数据库表达式执行的黑魔法：用Rust做类型体操之用宏展开重复代码 Part 3 & 4</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/数据库表达式执行的黑魔法：用Rust做类型体操/数据库表达式执行的黑魔法：与Rust编译器斗智斗勇之表达式向量化_Part_5_&_6.html"><strong aria-hidden="true">1.18.</strong> 数据库表达式执行的黑魔法：与Rust编译器斗智斗勇之表达式向量化 Part 5 & 6</a></li><li class="chapter-item expanded "><a href="../../01_类型系统/数据库表达式执行的黑魔法：用Rust做类型体操/数据库表达式执行的黑魔法：在Rust中用宏关联逻辑类型和实际类型_Part_7.html"><strong aria-hidden="true">1.19.</strong> 数据库表达式执行的黑魔法：在Rust中用宏关联逻辑类型和实际类型 Part 7</a></li></ol></li><li class="chapter-item expanded "><a href="../../empty.html"><strong aria-hidden="true">2.</strong> 生命周期</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../02_生命周期/Rust中的生命周期——从StrSplit实例说开去/Rust中的生命周期——从StrSplit实例说开去.html"><strong aria-hidden="true">2.1.</strong> Rust中的生命周期——从StrSplit实例说开去</a></li><li class="chapter-item expanded "><a href="../../02_生命周期/与ChatGPT深度对话来学Rust生命周期/与ChatGPT深度对话来学Rust生命周期.html"><strong aria-hidden="true">2.2.</strong> 与ChatGPT深度对话来学Rust生命周期</a></li><li class="chapter-item expanded "><a href="../../02_生命周期/进击的Rust生命周期——early_bound与late_bound（1）/进击的Rust生命周期——early_bound与late_bound（1）.html"><strong aria-hidden="true">2.3.</strong> 进击的Rust生命周期——early_bound与late_bound（1）</a></li><li class="chapter-item expanded "><a href="../../02_生命周期/进击的Rust生命周期——early_bound与late_bound（2）/进击的Rust生命周期——early_bound与late_bound（2）.html"><strong aria-hidden="true">2.4.</strong> 进击的Rust生命周期——early_bound与late_bound（2）</a></li><li class="chapter-item expanded "><a href="../../02_生命周期/进击的Rust生命周期——一力降十会的MIR（1）/进击的Rust生命周期——一力降十会的MIR（1）.html"><strong aria-hidden="true">2.5.</strong> 进击的Rust生命周期——一力降十会的MIR（1）</a></li><li class="chapter-item expanded "><a href="../../02_生命周期/进击的Rust生命周期——一力降十会的MIR（2）/进击的Rust生命周期——一力降十会的MIR（2）.html"><strong aria-hidden="true">2.6.</strong> 进击的Rust生命周期——一力降十会的MIR（2）</a></li><li class="chapter-item expanded "><a href="../../02_生命周期/Common_Rust_Lifetime_Misconceptions/Common_Rust_Lifetime_Misconceptions.html"><strong aria-hidden="true">2.7.</strong> Common Rust Lifetime Misconceptions</a></li><li class="chapter-item expanded "><a href="../../02_生命周期/Rust生命周期常见误区/Rust生命周期常见误区.html"><strong aria-hidden="true">2.8.</strong> 译：Rust生命周期常见误区</a></li></ol></li><li class="chapter-item expanded "><a href="../../empty.html"><strong aria-hidden="true">3.</strong> 无畏并发</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../05_无畏并发/简单写个Rust无锁队列/简单写个Rust无锁队列.html"><strong aria-hidden="true">3.1.</strong> 简单写个Rust无锁队列</a></li><li class="chapter-item expanded "><a href="../../05_无畏并发/进击的Rust多线程——混合自旋锁/进击的Rust多线程——混合自旋锁.html"><strong aria-hidden="true">3.2.</strong> 进击的Rust多线程——混合自旋锁</a></li><li class="chapter-item expanded "><a href="../../05_无畏并发/An_unsafe_tour_of_Rust's_Send_and_Sync/An_unsafe_tour_of_Rust's_Send_and_Sync.html"><strong aria-hidden="true">3.3.</strong> An unsafe tour of Rust's Send and Sync</a></li><li class="chapter-item expanded "><a href="../../05_无畏并发/进击的Rust多线程——Send与Sync/进击的Rust多线程——Send与Sync.html" class="active"><strong aria-hidden="true">3.4.</strong> 进击的Rust多线程——Send与Sync</a></li><li class="chapter-item expanded "><a href="../../05_无畏并发/进击的Rust多线程——离经叛道的PhantomData/进击的Rust多线程——离经叛道的PhantomData.html"><strong aria-hidden="true">3.5.</strong> 进击的Rust多线程——离经叛道的PhantomData</a></li><li class="chapter-item expanded "><a href="../../05_无畏并发/Rust_Async_Pin概念解析/Rust_Async_Pin概念解析.html"><strong aria-hidden="true">3.6.</strong> Rust Async: Pin概念解析</a></li><li class="chapter-item expanded "><a href="../../05_无畏并发/Rust和C++的并发库对比/Rust和C++的并发库对比.html"><strong aria-hidden="true">3.7.</strong> 译：Rust 和 C++ 的并发库对比</a></li><li class="chapter-item expanded "><a href="../../05_无畏并发/Rust原子类型和内存排序/Rust原子类型和内存排序.html"><strong aria-hidden="true">3.8.</strong> Rust原子类型和内存排序</a></li></ol></li><li class="chapter-item expanded "><a href="../../empty.html"><strong aria-hidden="true">4.</strong> 网络编程</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../06_网络编程/从编解码层面理解WebSocket_手写一个WebSocket/从编解码层面理解WebSocket_手写一个WebSocket.html"><strong aria-hidden="true">4.1.</strong> 从编解码层面理解WebSocket 手写一 个WebSocket</a></li><li class="chapter-item expanded "><a href="../../06_网络编程/透过Rust探索系统的本原：网络篇/透过Rust探索系统的本原：网络篇.html"><strong aria-hidden="true">4.2.</strong> 透过Rust探索系统的本原：网络篇</a></li></ol></li><li class="chapter-item expanded "><a href="../../empty.html"><strong aria-hidden="true">5.</strong> 轮子系列</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../07_轮子系列/700行Rust写一个内存分配器/700行Rust写一个内存分配器.html"><strong aria-hidden="true">5.1.</strong> 700行Rust写一个内存分配器</a></li><li class="chapter-item expanded "><a href="../../07_轮子系列/Rust：网络库的实现思路/Rust：网络库的实现思路.html"><strong aria-hidden="true">5.2.</strong> Rust：网络库的实现思路</a></li><li class="chapter-item expanded "><a href="../../07_轮子系列/Rust异步运行时基础部件/Rust异步运行时基础部件.html"><strong aria-hidden="true">5.3.</strong> Rust异步运行时基础部件</a></li><li class="chapter-item expanded "><a href="../../07_轮子系列/使用Rust+epoll编写异步IO框架/使用Rust+epoll编写异步IO框架（1）.html"><strong aria-hidden="true">5.4.</strong> 使用Rust+epoll编写异步IO框架（1）</a></li><li class="chapter-item expanded "><a href="../../07_轮子系列/使用Rust+epoll编写异步IO框架/使用Rust+epoll编写异步IO框架（2）.html"><strong aria-hidden="true">5.5.</strong> 使用Rust+epoll编写异步IO框架（2）</a></li><li class="chapter-item expanded "><a href="../../07_轮子系列/使用Rust+epoll编写异步IO框架/使用Rust+epoll编写异步IO框架（3）.html"><strong aria-hidden="true">5.6.</strong> 使用Rust+epoll编写异步IO框架（3）</a></li><li class="chapter-item expanded "><a href="../../07_轮子系列/用rust从零开发一套web框架/用rust从零开发一套web框架：day1.html"><strong aria-hidden="true">5.7.</strong> 用rust从零开发一套web框架：day1</a></li><li class="chapter-item expanded "><a href="../../07_轮子系列/用rust从零开发一套web框架/用rust从零开发一套web框架：day2.html"><strong aria-hidden="true">5.8.</strong> 用rust从零开发一套web框架：day2</a></li><li class="chapter-item expanded "><a href="../../07_轮子系列/用rust从零开发一套web框架/用rust从零开发一套web框架：day3.html"><strong aria-hidden="true">5.9.</strong> 用rust从零开发一套web框架：day3</a></li><li class="chapter-item expanded "><a href="../../07_轮子系列/用rust从零开发一套web框架/用rust从零开发一套web框架：day4.html"><strong aria-hidden="true">5.10.</strong> 用rust从零开发一套web框架：day4</a></li><li class="chapter-item expanded "><a href="../../07_轮子系列/用rust从零开发一套web框架/用rust从零开发一套web框架：day5.html"><strong aria-hidden="true">5.11.</strong> 用rust从零开发一套web框架：day5</a></li></ol></li><li class="chapter-item expanded "><a href="../../empty.html"><strong aria-hidden="true">6.</strong> 奇技淫巧</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../08_奇技淫巧/Copy-On-Write是不是优化？/Copy-On-Write是不是优化？.html"><strong aria-hidden="true">6.1.</strong> 译：Copy-On-Write是不是优化？</a></li><li class="chapter-item expanded "><a href="../../08_奇技淫巧/揭秘神奇的Rust_Axum风格的函数实现/揭秘神奇的Rust_Axum风格的函数实现.html"><strong aria-hidden="true">6.2.</strong> 译：揭秘神奇的 Rust Axum 风格的函数实现</a></li><li class="chapter-item expanded "><a href="../../08_奇技淫巧/“变长参数”函数与回调/“变长参数”函数与回调.html"><strong aria-hidden="true">6.3.</strong> “变长参数”函数与回调</a></li><li class="chapter-item expanded "><a href="../../08_奇技淫巧/Rust字符串格式化的幕后：format_args!()/Rust字符串格式化的幕后：format_args!().html"><strong aria-hidden="true">6.4.</strong> 译：Rust字符串格式化的幕后：format_args!()</a></li><li class="chapter-item expanded "><a href="../../08_奇技淫巧/给Rust带来一点C++特产/给Rust带来一点C++特产.html"><strong aria-hidden="true">6.5.</strong> 给Rust带来一点C++特产</a></li><li class="chapter-item expanded "><a href="../../08_奇技淫巧/一步步实现_Rust_Bevy_ECS_的_System_简化版本/一步步实现_Rust_Bevy_ECS_的_System_简化版本.html"><strong aria-hidden="true">6.6.</strong> 一步步实现 Rust Bevy ECS 的 System 简化版本</a></li><li class="chapter-item expanded "><a href="../../08_奇技淫巧/Exploring_Design_Patterns_in_Rust_with_Algorithmic_Trading_Examples/Exploring_Design_Patterns_in_Rust_with_Algorithmic_Trading_Examples.html"><strong aria-hidden="true">6.7.</strong> Exploring Design Patterns in Rust with Algorithmic Trading Examples</a></li></ol></li><li class="chapter-item expanded "><a href="../../empty.html"><strong aria-hidden="true">7.</strong> 源码分析</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../09_源码分析/Rust并发：bytes源码分析/Rust并发：bytes源码分析.html"><strong aria-hidden="true">7.1.</strong> Rust并发：bytes源码分析</a></li><li class="chapter-item expanded "><a href="../../09_源码分析/Rust并发：标准库Arc源码分析/Rust并发：标准库Arc源码分析.html"><strong aria-hidden="true">7.2.</strong> Rust并发：标准库Arc源码分析</a></li><li class="chapter-item expanded "><a href="../../09_源码分析/Rust并发：标准库sync_Once源码分析/Rust并发：标准库sync_Once源码分析.html"><strong aria-hidden="true">7.3.</strong> Rust并发：标准库sync::Once源码分析</a></li><li class="chapter-item expanded "><a href="../../09_源码分析/Rust源码阅读：引用计数Rc/Rust源码阅读：引用计数Rc.html"><strong aria-hidden="true">7.4.</strong> Rust源码阅读：引用计数Rc</a></li><li class="chapter-item expanded "><a href="../../09_源码分析/Rust源码阅读：Cell、RefCell与内部可变性/Rust源码阅读：Cell、RefCell与内部可变性.html"><strong aria-hidden="true">7.5.</strong> Rust源码阅读： Cell、RefCell与内部可变性</a></li><li class="chapter-item expanded "><a href="../../09_源码分析/关于_Rust_的_UnsafeCell、Cell_与_RefCell/关于_Rust_的_UnsafeCell、Cell_与_RefCell.html"><strong aria-hidden="true">7.6.</strong> 关于 Rust 的 UnsafeCell、Cell 与 RefCell</a></li><li class="chapter-item expanded "><a href="../../09_源码分析/Rust_Async_async-stream源码分析/Rust_Async_async-stream源码分析.html"><strong aria-hidden="true">7.7.</strong> Rust Async: async-stream源码分析</a></li><li class="chapter-item expanded "><a href="../../09_源码分析/走进Tokio的异步世界/走进Tokio的异步世界.html"><strong aria-hidden="true">7.8.</strong> 走进 Tokio 的异步世界</a></li><li class="chapter-item expanded "><a href="../../09_源码分析/tokio.rs_runtime的实现/tokio.rs_runtime的实现.html"><strong aria-hidden="true">7.9.</strong> tokio.rs runtime 的实现</a></li><li class="chapter-item expanded "><a href="../../09_源码分析/Tokio_internals/Tokio_internals.html"><strong aria-hidden="true">7.10.</strong> Tokio internals</a></li><li class="chapter-item expanded "><a href="../../09_源码分析/Tokio_internals/译文：Tokio內部机制.html"><strong aria-hidden="true">7.11.</strong> 译：Tokio 内部机制</a></li><li class="chapter-item expanded "><a href="../../09_源码分析/Rust_Axum_HTTP_框架的架构分析/Rust_Axum_HTTP_框架的架构分析.html"><strong aria-hidden="true">7.12.</strong> Rust Axum HTTP 框架的架构分析</a></li><li class="chapter-item expanded "><a href="../../09_源码分析/安利一个Rust_Game_Engine：Bevy--ECS部分/安利一个Rust_Game_Engine：Bevy--ECS部分.html"><strong aria-hidden="true">7.13.</strong> 安利一个Rust Game Engine：Bevy--ECS部分</a></li><li class="chapter-item expanded "><a href="../../09_源码分析/Tokio_解析之任务调度/Tokio_解析之任务调度.html"><strong aria-hidden="true">7.14.</strong> Tokio 解析之任务调度</a></li></ol></li><li class="chapter-item expanded "><a href="../../empty.html"><strong aria-hidden="true">8.</strong> 生态观察</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../11_生态观察/Rust_web_frameworks_have_subpar_error_reporting/Rust_web_frameworks_have_subpar_error_reporting.html"><strong aria-hidden="true">8.1.</strong> Rust web frameworks have subpar error reporting</a></li><li class="chapter-item expanded "><a href="../../11_生态观察/SeaORM：要做Rust版本的ActiveRecord/SeaORM：要做Rust版本的ActiveRecord.html"><strong aria-hidden="true">8.2.</strong> SeaORM：要做Rust版本的ActiveRecord</a></li></ol></li><li class="chapter-item expanded "><a href="../../empty.html"><strong aria-hidden="true">9.</strong> 死灵终极</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../12_死灵终极/Learn_Rust_the_Dangerous_Way_系列文章翻译/Learn_Rust_the_Dangerous_Way_系列文章翻译_总述.html"><strong aria-hidden="true">9.1.</strong> 译：Learn Rust the Dangerous Way 总述</a></li><li class="chapter-item expanded "><a href="../../12_死灵终极/Learn_Rust_the_Dangerous_Way_系列文章翻译/Learn_Rust_the_Dangerous_Way_系列文章翻译_0.html"><strong aria-hidden="true">9.2.</strong> 译：Learn Rust the Dangerous Way 0</a></li><li class="chapter-item expanded "><a href="../../12_死灵终极/Learn_Rust_the_Dangerous_Way_系列文章翻译/Learn_Rust_the_Dangerous_Way_系列文章翻译_1.html"><strong aria-hidden="true">9.3.</strong> 译：Learn Rust the Dangerous Way 1</a></li><li class="chapter-item expanded "><a href="../../12_死灵终极/Learn_Rust_the_Dangerous_Way_系列文章翻译/Learn_Rust_the_Dangerous_Way_系列文章翻译_2.html"><strong aria-hidden="true">9.4.</strong> 译：Learn Rust the Dangerous Way 2</a></li><li class="chapter-item expanded "><a href="../../12_死灵终极/Learn_Rust_the_Dangerous_Way_系列文章翻译/Learn_Rust_the_Dangerous_Way_系列文章翻译_3.html"><strong aria-hidden="true">9.5.</strong> 译：Learn Rust the Dangerous Way 3</a></li><li class="chapter-item expanded "><a href="../../12_死灵终极/Learn_Rust_the_Dangerous_Way_系列文章翻译/Learn_Rust_the_Dangerous_Way_系列文章翻译_4.html"><strong aria-hidden="true">9.6.</strong> 译：Learn Rust the Dangerous Way 4</a></li><li class="chapter-item expanded "><a href="../../12_死灵终极/Learn_Rust_the_Dangerous_Way_系列文章翻译/Learn_Rust_the_Dangerous_Way_系列文章翻译_5.html"><strong aria-hidden="true">9.7.</strong> 译：Learn Rust the Dangerous Way 5</a></li><li class="chapter-item expanded "><a href="../../12_死灵终极/Unsafe_Rust_随堂小测/Unsafe_Rust_随堂小测（一）.html"><strong aria-hidden="true">9.8.</strong> Unsafe Rust 随堂小测（一）</a></li><li class="chapter-item expanded "><a href="../../12_死灵终极/Unsafe_Rust_随堂小测/Unsafe_Rust_随堂小测（二）.html"><strong aria-hidden="true">9.9.</strong> Unsafe Rust 随堂小测（二）</a></li><li class="chapter-item expanded "><a href="../../12_死灵终极/Unsafe_Rust_随堂小测/Unsafe_Rust_随堂小测（三）.html"><strong aria-hidden="true">9.10.</strong> Unsafe Rust 随堂小测（三）</a></li><li class="chapter-item expanded "><a href="../../12_死灵终极/Unsafe_Rust_随堂小测/Unsafe_Rust_随堂小测参考答案.html"><strong aria-hidden="true">9.11.</strong> Unsafe Rust 随堂小测参考答案</a></li></ol></li></ol>
            </div>
            <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
        </nav>

        <div id="page-wrapper" class="page-wrapper">

            <div class="page">
                                <div id="menu-bar-hover-placeholder"></div>
                <div id="menu-bar" class="menu-bar sticky bordered">
                    <div class="left-buttons">
                        <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
                            <i class="fa fa-bars"></i>
                        </button>
                        <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
                            <i class="fa fa-paint-brush"></i>
                        </button>
                        <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
                            <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
                        </ul>
                        <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>

                    <h1 class="menu-title">Rust 优秀博文</h1>

                    <div class="right-buttons">
                        <a href="../../print.html" title="Print this book" aria-label="Print this book">
                            <i id="print-button" class="fa fa-print"></i>
                        </a>

                    </div>
                </div>

                <div id="search-wrapper" class="hidden">
                    <form id="searchbar-outer" class="searchbar-outer">
                        <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
                    </form>
                    <div id="searchresults-outer" class="searchresults-outer hidden">
                        <div id="searchresults-header" class="searchresults-header"></div>
                        <ul id="searchresults">
                        </ul>
                    </div>
                </div>

                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
                <script type="text/javascript">
                    document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });
                </script>

                <div id="content" class="content">
                    <main>
                        <h1 id="进击的rust多线程send与sync"><a class="header" href="#进击的rust多线程send与sync">进击的Rust多线程——Send与Sync</a></h1>
<p>作者：<a href="https://www.zhihu.com/people/you-ni-de-meng">有你的梦</a></p>
<p>原载：<a href="https://zhuanlan.zhihu.com/p/411873956">https://zhuanlan.zhihu.com/p/411873956</a></p>
<p>在Rust多线程中有两个重要的trait，Send和Sync。这两个trait大多数时候都是同时被实现的，但是并不是强相关，在一些特殊的使用场景中只有单个出现。下面贴出官方文档，不熟悉的一定要认真阅读。</p>
<p><a href="https://doc.rust-lang.org/std/marker/trait.Send.html#">https://doc.rust-lang.org/std/marker/trait.Send.html#</a></p>
<p><a href="https://doc.rust-lang.org/std/marker/trait.Sync.html#">https://doc.rust-lang.org/std/marker/trait.Sync.html#</a></p>
<p>简单解释下，实现了Send意味着该类型的所有权可以在不同的线程间传递，而实现了Sync则意味着该类型的引用可以在不同的线程间共享。即，Send是指对象在不同线程间传递，Sync是指不可变引用在不同线程间传递。</p>
<p>在这两个官方文档中都出现很诡异的一句话。</p>
<blockquote>
<p>This trait is automatically implemented when the compiler determines it’s appropriate.
当编译器确定它合适时，这个特性会自动实现。</p>
</blockquote>
<p>看到这里我就意识到一个很严重的问题，什么时候它合适呢？如果搞不懂这个，那我自定义的struct是否可以用在多线程编程中就是一个很大的问题了。</p>
<p><a href="https://doc.rust-lang.org/nomicon/send-and-sync.html#send-and-sync">https://doc.rust-lang.org/nomicon/send-and-sync.html#send-and-sync</a></p>
<p>在Rust死灵书中对应的相关章节相对详细一些。为了说明，我简单总结一下。</p>
<p>大多数的原生类型都自动实现了<code>Send</code>和<code>Sync</code>，但是有几个例外：</p>
<ul>
<li>原始指针既不是<code>Send</code>也不是<code>Sync</code>（因为它们没有安全防护）。</li>
<li><code>UnsafeCell</code>不是<code>Sync</code>（因此<code>Cell</code>和<code>RefCell</code>不是）。</li>
<li><code>Rc</code>不是<code>Send</code>或<code>Sync</code>(因为<code>refcount</code>是共享和非同步的)。</li>
</ul>
<p>简单看了一下一部分标准库代码，又发现了一些值得推敲的东西。下面分享给大家。</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub struct Arc&lt;T: ?Sized&gt; {
    ptr: NonNull&lt;ArcInner&lt;T&gt;&gt;,
    phantom: PhantomData&lt;ArcInner&lt;T&gt;&gt;,
}

#[stable(feature = &quot;rust1&quot;, since = &quot;1.0.0&quot;)]
unsafe impl&lt;T: ?Sized + Sync + Send&gt; Send for Arc&lt;T&gt; {}
#[stable(feature = &quot;rust1&quot;, since = &quot;1.0.0&quot;)]
unsafe impl&lt;T: ?Sized + Sync + Send&gt; Sync for Arc&lt;T&gt; {}
//.....省略部分代码
struct ArcInner&lt;T: ?Sized&gt; {
    strong: atomic::AtomicUsize,

    // the value usize::MAX acts as a sentinel for temporarily &quot;locking&quot; the
    // ability to upgrade weak pointers or downgrade strong ones; this is used
    // to avoid races in `make_mut` and `get_mut`.
    weak: atomic::AtomicUsize,

    data: T,
}
unsafe impl&lt;T: ?Sized + Sync + Send&gt; Send for ArcInner&lt;T&gt; {}
unsafe impl&lt;T: ?Sized + Sync + Send&gt; Sync for ArcInner&lt;T&gt; {}
<span class="boring">}
</span></code></pre></pre>
<p>这一段是Arc的源码，主要截取了<code>Arc</code>和包含的<code>ArcInner</code>的Struct结构以及对应的<code>Send</code>和<code>Sync</code>的实现。但是官方说的是自动推断啊，为啥这里是显式的声明？</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>#[stable(feature = &quot;rust1&quot;, since = &quot;1.0.0&quot;)]
unsafe impl&lt;T: Send&gt; Send for LinkedList&lt;T&gt; {}

#[stable(feature = &quot;rust1&quot;, since = &quot;1.0.0&quot;)]
unsafe impl&lt;T: Sync&gt; Sync for LinkedList&lt;T&gt; {}

#[stable(feature = &quot;rust1&quot;, since = &quot;1.0.0&quot;)]
unsafe impl&lt;T: Sync&gt; Send for Iter&lt;'_, T&gt; {}

#[stable(feature = &quot;rust1&quot;, since = &quot;1.0.0&quot;)]
unsafe impl&lt;T: Sync&gt; Sync for Iter&lt;'_, T&gt; {}

#[stable(feature = &quot;rust1&quot;, since = &quot;1.0.0&quot;)]
unsafe impl&lt;T: Send&gt; Send for IterMut&lt;'_, T&gt; {}

#[stable(feature = &quot;rust1&quot;, since = &quot;1.0.0&quot;)]
unsafe impl&lt;T: Sync&gt; Sync for IterMut&lt;'_, T&gt; {}

#[unstable(feature = &quot;linked_list_cursors&quot;, issue = &quot;58533&quot;)]
unsafe impl&lt;T: Sync&gt; Send for Cursor&lt;'_, T&gt; {}

#[unstable(feature = &quot;linked_list_cursors&quot;, issue = &quot;58533&quot;)]
unsafe impl&lt;T: Sync&gt; Sync for Cursor&lt;'_, T&gt; {}

#[unstable(feature = &quot;linked_list_cursors&quot;, issue = &quot;58533&quot;)]
unsafe impl&lt;T: Send&gt; Send for CursorMut&lt;'_, T&gt; {}

#[unstable(feature = &quot;linked_list_cursors&quot;, issue = &quot;58533&quot;)]
unsafe impl&lt;T: Sync&gt; Sync for CursorMut&lt;'_, T&gt; {}
<span class="boring">}
</span></code></pre></pre>
<p>还有这里，大家有没有注意到第8、20行中，T的约束为Sync与实现的Send并不一致。这里又意味着什么呢？</p>
<p>隐约觉得不简单。我们一个一个来，首先是编译器推断规则的验证。</p>
<h2 id="1编译器推断sync与send的实现规则"><a class="header" href="#1编译器推断sync与send的实现规则">1、编译器推断Sync与Send的实现规则</a></h2>
<h2 id="代码1-1"><a class="header" href="#代码1-1">代码1-1：</a></h2>
<pre><pre class="playground"><code class="language-rust">use std::thread;

struct Foo&lt;T&gt;{
    b: T
}

fn main() {
    let five = Foo{
        b: 5
    };
    thread::spawn(move || {
        five
    });
}
</code></pre></pre>
<p>编译之后正常，得出结论：<strong>如果复合类型中内含的所有类型都实现了</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>，则编译器推导复合类型自动实现</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>。</strong></p>
<h2 id="代码1-2"><a class="header" href="#代码1-2">代码1-2：</a></h2>
<pre><pre class="playground"><code class="language-rust">use std::thread;

struct Foo&lt;T&gt;{
    a: *const T,
    b: T
}

fn main() {
    let a_box = Box::into_raw(Box::new(5));
    let five = Foo{
        a: a_box,
        b: 5
    };
    thread::spawn(move || {
        five
    });
}
</code></pre></pre>
<p>编译之后报错：<code>error[E0277]: *const {integer} cannot be sent between threads safely</code></p>
<p>结论：<strong>如果复合类型中内含的类型中出现了未实现</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>的类型，则编译器推导复合类型没有实现</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>。</strong></p>
<h2 id="代码1-3"><a class="header" href="#代码1-3">代码1-3：</a></h2>
<pre><pre class="playground"><code class="language-rust">use std::thread;

struct Foo&lt;T&gt;{
    a: *const T,
    b: T
}

unsafe impl&lt;T&gt; Send for Foo&lt;T&gt; {}
unsafe impl&lt;T&gt; Sync for Foo&lt;T&gt; {}

fn main() {
    let a_box = Box::into_raw(Box::new(5));
    let five = Foo{
        a: a_box,
        b: 5
    };
    thread::spawn(move || {
        five
    });
}
</code></pre></pre>
<p>代码编译通过，但是机智的我在编译结果中发现了一个细微的问题。</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>Compiling playground v0.0.1 (/playground)
warning: field is never read: `a`
 --&gt; src/main.rs:4:5
  |
4 |     a: *const T,
  |     ^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: field is never read: `b`
 --&gt; src/main.rs:5:5
  |
5 |     b: T
  |     ^^^^

warning: `playground` (bin &quot;playground&quot;) generated 2 warnings
<span class="boring">}
</span></code></pre></pre>
<p>Struct中的属性从未被读到，前面两次编译时都没有提示过这个警告，所以这里应该有猫腻。</p>
<h2 id="代码1-4"><a class="header" href="#代码1-4">代码1-4：</a></h2>
<pre><pre class="playground"><code class="language-rust">use std::thread;

struct Foo&lt;T&gt;{
    a: *const T,
    b: T
}

unsafe impl&lt;T&gt; Send for Foo&lt;T&gt; {}
unsafe impl&lt;T&gt; Sync for Foo&lt;T&gt; {}

fn main() {
    let a_box = Box::into_raw(Box::new(5));
    let five = Foo{
        a: a_box,
        b: 5
    };
    thread::spawn(move || {
        five.a
    });
}
</code></pre></pre>
<p>和代码1-3相比只改了第18行，我在子线程中使用了Foo的a。编译果然报错：<code>*const {integer} cannot be sent between threads safely.</code></p>
<h2 id="乘胜追击代码1-5"><a class="header" href="#乘胜追击代码1-5">乘胜追击，代码1-5：</a></h2>
<pre><pre class="playground"><code class="language-rust">use std::thread;

struct Foo&lt;T&gt;{
    a: *const T,
    b: T
}

unsafe impl&lt;T&gt; Send for Foo&lt;T&gt; {}
unsafe impl&lt;T&gt; Sync for Foo&lt;T&gt; {}

fn main() {
    let a_box = Box::into_raw(Box::new(5));
    let five = Foo{
        a: a_box,
        b: 5
    };
    thread::spawn(move || {
        five.b
    });
}
</code></pre></pre>
<p>改了第18行，我在子线程中使用了Foo的b。编译之后通过。依然出现了警告，five的a没有被使用。得出结论：<strong>复合类型中内含的类型中出现了未实现</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>的类型，但我们为此复合类型显式实现了这两个类型。如果子线程中没有使用未实现</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>的类型，则编译器推断此复合类型为实现了</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>的类型。</strong></p>
<h2 id="总结"><a class="header" href="#总结">总结</a></h2>
<ul>
<li>
<p><strong>复合类型中内含的所有类型都实现了</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>，</strong></p>
</li>
<li>
<p><strong>编译器推导复合类型实现</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>。（代码1-1）</strong></p>
</li>
<li>
<p><strong>如果复合类型中内含的类型中出现了未实现</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>的类型，</strong></p>
</li>
<li>
<p><strong>没有手动显式实现</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>，</strong></p>
</li>
<li>
<ul>
<li><strong>编译器推导复合类型没有实现</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>。（代码1-2）</strong></li>
</ul>
</li>
<li>
<p><strong>手动显式实现了</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>，</strong></p>
</li>
<li>
<ul>
<li><strong>子线程中未使用复合类型中未实现</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>的类型，</strong></li>
<li><strong>编译器推导复合类型实现了</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>。（代码1-3，代码1-5）</strong></li>
<li><strong>子线程中使用了复合类型中未实现</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>的类型，</strong></li>
<li><strong>编译器推导复合类型没有实现</strong><code>Send</code><strong>和</strong><code>Sync</code><strong>。（代码1-4）</strong></li>
</ul>
</li>
</ul>
<p>也就是说，如果手动实现了<code>Send</code>和<code>Sync</code>，则Rust是无法保证线程安全的，需要程序员多加小心，以防止在多线程中使用未实现<code>Send</code>和<code>Sync</code>的类型而引起的panic。</p>
<h2 id="2泛型的约束关系与实现"><a class="header" href="#2泛型的约束关系与实现">2、泛型的约束关系与实现</a></h2>
<p>首先思考<code>Send</code>与<code>Sync</code>的关系，这边我找到了两篇博客，希望大家一定要读一下。</p>
<p><a href="https://nyanpasu64.github.io/blog/an-unsafe-tour-of-rust-s-send-and-sync">https://nyanpasu64.github.io/blog/an-unsafe-tour-of-rust-s-send-and-sync</a></p>
<p>文章中有一些重要的描述，我直接引用过来了：</p>
<blockquote>
<p><code>T: Send</code> means <code>T</code> and <code>&amp;mut T</code> (which allow dropping <code>T</code>) can be passed between threads. <code>T: Sync</code> means <code>&amp;T</code> (which allows shared/aliased access to <code>T</code>) can be passed between threads. Either or both may be true for any given type. <code>T: Sync</code> ≡ <code>&amp;T: Send</code> (by definition).
<code>T: Send</code>表示<code>T</code>和<code>&amp;mut</code>(允许丢弃<code>T</code>)可以在线程之间传递。<code>T: Sync</code>意味着<code>&amp;T</code>(允许共享/别名访问<code>T</code>)可以在线程之间传递。对于任何给定类型，其中一个或两个都可能为真。<code>T: Sync</code>与<code>&amp;T: Send</code>完全等价(根据定义)。</p>
</blockquote>
<p>其实泛型约束关系与实现总共有四种情况，泛型约束与实现一致的情况，其实很好理解。T为Send约束，则Foo<T>也满足Send约束这没什么问题。我们把关注点放在不一致的约束上，那就剩两种情况了：</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>unsafe impl&lt;'a, T: Sync&gt; Send for Foo&lt;T&gt; {}
unsafe impl&lt;'a, T: Send&gt; Sync for Foo&lt;T&gt; {}
<span class="boring">}
</span></code></pre></pre>
<p>分别来看：</p>
<h2 id="unsafe-impla-t-sync-send-for-foot-"><a class="header" href="#unsafe-impla-t-sync-send-for-foot-"><code>unsafe impl&lt;'a, T: Sync&gt; Send for Foo&lt;T&gt; {}</code></a></h2>
<p>表达的含义是：<strong>Foo需要在线程中进行所有权的传递，则T必须允许在线程中以共享引用的方式传递。</strong></p>
<p>按照上文给的全等关系代入，<code>T: Sync</code>那么<code>&amp;T: Send</code>，所以<code>unsafe impl&lt;'a, T: Sync&gt; Send for Foo&lt;T&gt; {}</code>就可以转成<code>unsafe impl&lt;'a, &amp;T: Send&gt; Send for Foo&lt;T&gt; {}</code>也就是说，<code>Foo&lt;T&gt;</code>中并不涉及到<code>T</code>的所有权，只使用了<code>T</code>的共享引用，那么<strong>Foo需要在线程中进行所有权的传递，只需要T满足引用的传递就可以了。结论：当T在Foo中以不变引用的方式存在时，此约束成立。</strong></p>
<p>标准库源码例子：</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub struct Cursor&lt;'a, T: 'a&gt; {
    index: usize,
    current: Option&lt;NonNull&lt;Node&lt;T&gt;&gt;&gt;,
    list: &amp;'a LinkedList&lt;T&gt;,
}
#[unstable(feature = &quot;linked_list_cursors&quot;, issue = &quot;58533&quot;)]
unsafe impl&lt;T: Sync&gt; Send for Cursor&lt;'_, T&gt; {}

#[unstable(feature = &quot;linked_list_cursors&quot;, issue = &quot;58533&quot;)]
unsafe impl&lt;T: Sync&gt; Sync for Cursor&lt;'_, T&gt; {}
<span class="boring">}
</span></code></pre></pre>
<h2 id="unsafe-impla-t-send-sync-for-foot-"><a class="header" href="#unsafe-impla-t-send-sync-for-foot-"><code>unsafe impl&lt;'a, T: Send&gt; Sync for Foo&lt;T&gt; {}</code></a></h2>
<p>表达的含义是：**Foo需要在线程中以共享引用的方式传递，则T必须允许在线程中传递所有权。<strong>按照上文给的全等关系代入，<code>T: Sync</code>那么<code>&amp;T: Send</code>，所以<code>unsafe impl&lt;'a, T: Send&gt; Sync for Foo&lt;T&gt; {}</code>就可以转成<code>unsafe impl&lt;'a, T: Send&gt; Send for &amp;Foo&lt;T&gt; {}</code>也就是说，虽然T只允许在多个线程中独占式的传递，但是通过<code>Foo</code>的包装，就可以将T以共享引用的方式同时分发给多个线程。那么</strong>Foo只要满足同时只允许一个线程操作T，那么就可以使一个本身只是允许独占操作的的T，以共享的方式在线程中传递了。**结论：<strong>当Foo中存在限制多个线程同时操作T的能力时，此约束成立。</strong></p>
<p>什么叫限制多个线程同时操作T的能力？那不就是锁么？没错，标准库中的<code>Mutex</code>就是这么玩的：</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub struct Mutex&lt;T: ?Sized&gt; {
    inner: sys::MovableMutex,
    poison: poison::Flag,
    data: UnsafeCell&lt;T&gt;,
}

// these are the only places where `T: Send` matters; all other
// functionality works fine on a single thread.
#[stable(feature = &quot;rust1&quot;, since = &quot;1.0.0&quot;)]
unsafe impl&lt;T: ?Sized + Send&gt; Send for Mutex&lt;T&gt; {}
#[stable(feature = &quot;rust1&quot;, since = &quot;1.0.0&quot;)]
unsafe impl&lt;T: ?Sized + Send&gt; Sync for Mutex&lt;T&gt; {}
<span class="boring">}
</span></code></pre></pre>
<p>编辑于 2021-09-18 21:06</p>

                    </main>

                    <nav class="nav-wrapper" aria-label="Page navigation">
                        <!-- Mobile navigation buttons -->
                            <a rel="prev" href="../../05_无畏并发/An_unsafe_tour_of_Rust&#x27;s_Send_and_Sync/An_unsafe_tour_of_Rust&#x27;s_Send_and_Sync.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                                <i class="fa fa-angle-left"></i>
                            </a>

                            <a rel="next" href="../../05_无畏并发/进击的Rust多线程——离经叛道的PhantomData/进击的Rust多线程——离经叛道的PhantomData.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                                <i class="fa fa-angle-right"></i>
                            </a>

                        <div style="clear: both"></div>
                    </nav>
                </div>
            </div>

            <nav class="nav-wide-wrapper" aria-label="Page navigation">
                    <a rel="prev" href="../../05_无畏并发/An_unsafe_tour_of_Rust&#x27;s_Send_and_Sync/An_unsafe_tour_of_Rust&#x27;s_Send_and_Sync.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                        <i class="fa fa-angle-left"></i>
                    </a>

                    <a rel="next" href="../../05_无畏并发/进击的Rust多线程——离经叛道的PhantomData/进击的Rust多线程——离经叛道的PhantomData.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                        <i class="fa fa-angle-right"></i>
                    </a>
            </nav>

        </div>




        <script type="text/javascript">
            window.playground_copyable = true;
        </script>


        <script src="../../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../../mark.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../../searcher.js" type="text/javascript" charset="utf-8"></script>

        <script src="../../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../../highlight.js" type="text/javascript" charset="utf-8"></script>
        <script src="../../book.js" type="text/javascript" charset="utf-8"></script>

        <!-- Custom JS scripts -->


    </body>
</html>
